/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.motherfoca.DAO;

import com.motherfoca.VO.COpciones;
import com.motherfoca.VO.CPregunta;
import com.motherfoca.VO.CTema;
import com.motherfoca.VO.CUsuario;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author langeblanc
 */
public class CMotherFocaDAOSingleton {

    private EntityManagerFactory emf;
    private EntityManager em;
    private EntityTransaction trans;
    private boolean flag;
    public static CMotherFocaDAOSingleton instance = new CMotherFocaDAOSingleton();

    private CMotherFocaDAOSingleton() {
        emf = Persistence.createEntityManagerFactory("MotherFocaPU");
        em = emf.createEntityManager();
        trans = em.getTransaction();
    }

    public boolean insertar(Object obj) {
        try {
            trans.begin();
            em.persist(obj);
            trans.commit();
            flag = true;
        } catch (Exception ex) {
            trans.rollback();
            flag = false;
            System.out.println(ex.getMessage());
        }
        return flag;
    }

    public boolean actualizar(Object obj) {
        try {
            trans.begin();
            em.merge(obj);
            trans.commit();
            flag = true;
        } catch (Exception ex) {
            trans.rollback();
            flag = false;
        }
        return flag;
    }

    public boolean eliminar(Object obj) {
        try {
            trans.begin();
            em.remove(em.merge(obj));
            trans.commit();
            flag = true;
        } catch (Exception ex) {
            trans.rollback();
            flag = false;
        }
        return flag;
    }

    public Object consultarPorId(Object id) {
        throw new UnsupportedOperationException("ya casi jeje");
    }

    public CUsuario getUsuarioByUser(String usuario) {
        Query query = null;
        try {
            query = em.createNamedQuery("CUsuario.findByNombreUsuario");
            query.setParameter("nombreUsuario", usuario);
        } catch (Exception ex) {
            throw new Exception("Error en la BD");
        } finally {
            return (CUsuario) query.getSingleResult();
        }
    }

    public List<CUsuario> getUsuarioByRol(String rol) {
        Query query = null;
        try {
            query = em.createNamedQuery("CUsuario.findByRol");
            query.setParameter("rol", rol);
        } catch (Exception ex) {
            throw new Exception("Error en la BD");
        } finally {
            return query.getResultList();
        }
    }

    public List<CTema> getAllTemas() {
        Query query = null;
        try {
            query = em.createNamedQuery("CTema.findAll");
        } catch (Exception ex) {
            throw new Exception("Error en la BD");
        } finally {
            return query.getResultList();
        }
    }

    public List<CTema> getTemaLikeName(String tema) {
        Query query = null;
        try {
            query = em.createNamedQuery("CTema.findLikeNombre");
            query.setParameter("nombre", tema);
        } catch (Exception ex) {
            throw new Exception("Error en la BD");
        } finally {
            return query.getResultList();
        }
    }

    public CTema getTemaByName(String tema) {
        Query query = null;
        try {
            query = em.createNamedQuery("CTema.findByNombre");
            query.setParameter("nombre", tema);
        } catch (Exception ex) {
            throw new Exception("Error en la BD");
        } finally {
            return (CTema) query.getSingleResult();
        }
    }

    public List<CPregunta> getAllPreguntasHavingTema(int tema) {
        Query query = null;
        try {
            query = em.createNamedQuery("CPregunta.findByTemaIdtema");
            query.setParameter("temaIdtema", tema);
        } catch (Exception ex) {
            throw new Exception("Error en la BD");
        } finally {
            return query.getResultList();
        }
    }

    public List<COpciones> getAllOpcionesHavingPregunta(CPregunta pregunta) {
        Query query = null;
        try {
            query = em.createNamedQuery("COpciones.findByPreguntaNumeroPregunta");
            query.setParameter("numeroPregunta", pregunta.getCPreguntaPK().getNumeroPregunta());
        } catch (Exception ex) {
            throw new Exception("Error en la BD");
        } finally {
            return query.getResultList();
        }
    }

    public static CMotherFocaDAOSingleton getInstance() {
        return instance;
    }
}
